package com.skcc.cop.mrtaxi.biz.destination;

import java.io.Serializable;
import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.skcc.cop.mrtaxi.util.DatabaseUtil;


/**
 * @author tmkim
 * 
 */
public class DestinationEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	public final static String TABLE_NAME = "destination";
	public final static String CREATE_TABLE = "CREATE TABLE ["
			+ TABLE_NAME
			+ "] ( [id] INTEGER PRIMARY KEY, [address1] TEXT, [city] TEXT, [district] TEXT, [street] TEXT, [passenger] INTEGER, "
			+ "[lati] DOUBLE,[longi] DOUBLE, [accuracy] DOUBLE, [regTime] TEXT)";
	public final static String[] PROJECT = { "id", "address1", "city", "district", "street", "passenger", "lati", "longi",
		"accuracy", "regTime" };


	private int id;
	private String address1 = "";
	private String city = "";
	private String district = "";
	private String street = "";
	private int passenger = 0;
	private double lati = 0.0;
	private double longi = 0.0;
	private double accuracy = 0.0;
	private String regTime;

	public DestinationEntity() {

	}
	
	public DestinationEntity(String address1, String city, String district, String street, int passenger, 
			double lati, double longi, double accuracy, String regTime){
		this.address1 = address1;
		this.city = city;
		this.district = district;
		this.street = street;
		this.passenger = passenger;
		this.lati = lati;
		this.longi = longi;
		this.accuracy = accuracy;
		this.regTime = regTime;
	}

	public DestinationEntity(Cursor cursor) {
		setId(DatabaseUtil.getIntValue(cursor, "id"));
		setAddress1(DatabaseUtil.getStringValue(cursor, "address1"));
		setCity(DatabaseUtil.getStringValue(cursor, "city"));
		setDistrict(DatabaseUtil.getStringValue(cursor, "district"));
		setStreet(DatabaseUtil.getStringValue(cursor, "street"));
		setPassenger(DatabaseUtil.getIntValue(cursor, "passenger"));
		setLati(DatabaseUtil.getDoubleValue(cursor, "lati"));
		setLongi(DatabaseUtil.getDoubleValue(cursor, "longi"));
		setAccuracy(DatabaseUtil.getDoubleValue(cursor, "accuracy"));
		setRegTime(DatabaseUtil.getStringValue(cursor, "regTime"));
	}

	public boolean insertEntity(SQLiteDatabase database, String address1, String city, String district, String street, int passenger, 
			double lati, double longi, double accuracy, String regTime) {
		ContentValues conVal = new ContentValues();
		// id는 없어야 한다
		conVal.put("address1", address1);
		conVal.put("city", city);
		conVal.put("district", district);
		conVal.put("street", street);
		conVal.put("passenger", passenger);
		conVal.put("lati", lati);
		conVal.put("longi", longi);
		conVal.put("accuracy", accuracy);
		conVal.put("regTime", regTime);

		return DatabaseUtil.insert(database, TABLE_NAME, conVal);
	}

	public boolean insertEntity(SQLiteDatabase database, ArrayList<DestinationEntity> destinationEntity) {

		try{
			database.beginTransaction();
			for(DestinationEntity entity : destinationEntity){
				ContentValues conVal = new ContentValues();
				conVal.put("address1", entity.getAddress1());
				conVal.put("city", entity.getCity());
				conVal.put("district", entity.getDistrict());
				conVal.put("street", entity.getStreet());
				conVal.put("passenger", entity.getPassenger());
				conVal.put("lati", entity.getLati());
				conVal.put("longi", entity.getLongi());
				conVal.put("accuracy", entity.getAccuracy());
				conVal.put("regTime", entity.getRegTime());
				database.insert(TABLE_NAME, null, conVal);
			}
			database.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			database.endTransaction();
		}

		return true;

	}

	public boolean updateEntity(SQLiteDatabase database, int id, String address1, String city, String district, String street, int passenger, 
			double lati, double longi, double accuracy) {
		String where = "id = ?";
		String[] whereClause = new String[] { String.valueOf(id) };
		ContentValues conVal = new ContentValues();

		conVal.put("address1", address1);
		conVal.put("city", city);
		conVal.put("district", district);
		conVal.put("street", street);
		conVal.put("passenger", passenger);
		conVal.put("lati", lati);
		conVal.put("longi", longi);
		conVal.put("accuracy", accuracy);
		conVal.put("regTime", regTime);
		
		return DatabaseUtil.update(database, TABLE_NAME, conVal, where, whereClause);
	}

	public boolean updateEntity(SQLiteDatabase database, ArrayList<DestinationEntity> destinationEntity) {
		try{
			database.beginTransaction();
			for(DestinationEntity entity : destinationEntity){
				ContentValues conVal = new ContentValues();
				conVal.put("address1", entity.getAddress1());
				conVal.put("city", entity.getCity());
				conVal.put("district", entity.getDistrict());
				conVal.put("street", entity.getStreet());
				conVal.put("passenger", entity.getPassenger());
				conVal.put("lati", entity.getLati());
				conVal.put("longi", entity.getLongi());
				conVal.put("accuracy", entity.getAccuracy());
				conVal.put("regTime", entity.getRegTime());
				database.insert(TABLE_NAME, null, conVal);
			}
			database.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			database.endTransaction();
		}

		return true;
	}

	public boolean deleteEntity(SQLiteDatabase database, int id) {
		String where = "id = ?";
		String[] whereClause = new String[] { String.valueOf(id) };

		return DatabaseUtil.delete(database, TABLE_NAME, where, whereClause);
	}

	public ArrayList<DestinationEntity> getEntity(SQLiteDatabase database) {
		String where = null;
		String[] whereClause = null;

		Cursor cursor = database.query(TABLE_NAME, PROJECT, where, whereClause, null, null, null);
		ArrayList<DestinationEntity> dataList = makeArrayList(cursor);

		cursor.close();
		return dataList;
	}

	private ArrayList<DestinationEntity> makeArrayList(Cursor cursor) {
		ArrayList<DestinationEntity> dataList = new ArrayList<DestinationEntity>();
		while (cursor.moveToNext()) {
			DestinationEntity msgData = new DestinationEntity(cursor);
			dataList.add(msgData);
		}
		return dataList;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAddress1() {
		return address1;
	}

	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public int getPassenger() {
		return passenger;
	}

	public void setPassenger(int passenger) {
		this.passenger = passenger;
	}

	public double getLati() {
		return lati;
	}

	public void setLati(double lati) {
		this.lati = lati;
	}

	public double getLongi() {
		return longi;
	}

	public void setLongi(double longi) {
		this.longi = longi;
	}

	public double getAccuracy() {
		return accuracy;
	}

	public void setAccuracy(double accuracy) {
		this.accuracy = accuracy;
	}

	public String getRegTime() {
		return regTime;
	}

	public void setRegTime(String regTime) {
		this.regTime = regTime;
	}



}
